<?php
namespace app\admin\controller;
use think\Db;
use app\admin\controller\Auth;
use think\Config;
class Business extends Auth{
	public function addBusiness(){
		return $this->fetch();
	}
	public function businessList(){
		$list = Db::name('business')->paginate(10);
		$this->assign('list',$list);
		return $this->fetch();
	}
	// 主营业务详情
	public function businessInfo(){
		$id = input('bid');
		if(empty($id)){
			$this->error('禁止非法操作');
			exit;
		}
		$info = Db::name('business')->where('id = ' . $id)->find();
		if(empty($info)){
			$this->error('该主营业务不存在');
			exit;
		}
		$this->assign('info',$info);
		return $this->fetch();
	}
	// 执行添加业务分类
	public function doAddBusiness(){
		$title = trim(input('post.title'));
		$content = trim(input('post.content'));
		$info = trim(input('post.info'));
		if(empty($title) || empty($content) || empty($info)){
			$this->error('信息不完善');
			exit;
		}
		$image = request()->file('image');
		if(empty($image)){
			$this->error('请选择图片');
			exit;
		}
		$info = $image->move(ROOT_PATH . 'public' . DS . 'uploads');
		if($info){
			// 成功上传后 获取上传信息
			// 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
			$data['logo'] = Config::get('view_replace_str.__UPLOAD__').'/'.$info->getSaveName();
		}else{
			$this->error($image->getError());
			exit;
		}
		$data['title'] = $title;
		$data['content'] = $content;
		$data['info'] = $info;
		$data['createtime'] = time();
		$result = Db::name('business')->insert($data);
		if($result){
			$this->redirect('/admin/business/addBusiness');
		}else{
			$this->error('添加失败');
		}
	}
	// 删除主营业务分类
	public function delBusiness(){
		$bid = input('post.bid');
		if(empty($bid)){
			$this->error('禁止非法操作');
			exit;
		}
		$where['id'] = array('EQ',$bid);
		$result = Db::name('business')->where($where)->delete();
		if($result){
			$this->redirect('/admin/business/businessList');
		}else{
			$this->error('删除失败');
		}
	}
	// 主营业务logo
	public function businessLogo(){
		$logo = Db::name('navigation_logo')->where('type = 1')->value('logo');
		$this->assign('logo',$logo);
		return $this->fetch();
	}
	// 修改主营业务logo
	public function updateBusinessLogo(){
		$image = request()->file('image');
		if(empty($image)){
			$this->error('请选择图片');
			exit;
		}
		$info = $image->move(ROOT_PATH . 'public' . DS . 'uploads');
		if($info){
			// 成功上传后 获取上传信息
			// 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
			$data['logo'] = Config::get('view_replace_str.__UPLOAD__').'/'.$info->getSaveName();
		}else{
			$this->error($image->getError());
			exit;
		}
		$where['type'] = array('EQ',1);
		$data['createtime'] = time();
		$result = Db::name('navigation_logo')->where($where)->update($data);
		if($result){
			$this->redirect('/admin/business/businessLogo');
		}else{
			$this->error('修改失败');
		}
	}
	// 修改主营业务
	public function updateBusiness(){
		$id = input('post.id');
		if(empty($id)){
			$this->error('禁止非法操作');
			exit;
		}else{
			$where['id'] = array('EQ',$id);
		}
		$title = trim(input('post.title'));
		if(empty($title)){
			$this->error('标题不能为空');
			exit;
		}else{
			$data['title'] = $title;
		}
		$content = trim(input('post.content'));
		if(empty($content)){
			$this->error('介绍不能为空');
			exit;
		}else{
			$data['content'] = $content;
		}
		$info = input('post.info');
		if(empty($info)){
			$this->error('详情不能为空');
			exit;
		}else{
			$data['info'] = $info;
		}
		$image = request()->file('image');
		if(!empty($image)){
			$info = $image->move(ROOT_PATH . 'public' . DS . 'uploads');
			if($info){
				// 成功上传后 获取上传信息
				// 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
				$data['logo'] = Config::get('view_replace_str.__UPLOAD__').'/'.$info->getSaveName();
			}else{
				$this->error($image->getError());
				exit;
			}
		}
		$result = Db::name('business')->where($where)->update($data);
		if($result){
			$this->redirect('/admin/business/businessInfo/bid/'.$id);
		}else{
			$this->error('修改失败');
		}
	}
} 